<%= include ic/admin-header.html %>

<div class="am-cf admin-main">
  <%= include ic/admin-sidebar.html %>

  <!-- content start -->
  <div class="admin-content">

    <div class="am-cf am-padding">
      <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">标签管理</strong> / <small><%= name %></small></div>
    </div>

    <div class="am-g">
      <div class="am-u-sm-12 am-u-md-4">
        <div class="am-input-group am-input-group-sm">
          <input id="newTag" type="text" class="am-form-field">
          <span class="am-input-group-btn">
            <button class="am-btn am-btn-default" type="button" onclick="addTag()">新增</button>
          </span>
        </div>
      </div>
      <div class="am-u-sm-12 am-u-md-offset-5 am-u-md-3">
        <div class="am-btn-toolbar">
          <div class="am-btn-group am-btn-group-xs">
            <button class="am-btn am-btn-default btn-saveEdit" type="button" onclick="save()">保存修改</button>
          </div>
        </div>
      </div>
    </div>
    <hr />
    <div class="am-g">
      <div class="am-u-sm-12" id="tagList">
      <% for(var i=0;i<results.length;i++){ %>
      <span val="<%= results[i] %>" class="tag"><%= results[i] %> <a href="javascript:void(0)" class="am-close" onclick="delTag(this)">&times;</a></span>
      <% } %>
      </div>
    </div>
  </div>
  <!-- content end -->
</div>
<a class="am-icon-btn am-icon-th-list am-show-sm-only admin-menu" data-am-offcanvas="{target: '#admin-offcanvas'}"></a>
<style>
#tagList{padding-bottom:20px;}
.tag{padding:3px;border:1px solid #999;margin:5px 5px 0 0;background:#eee;color:#333;float:left;}
</style>
<%= include ic/admin-footer.html %>
<script type="text/javascript">
$(function() {
	$("#collapse-setting").addClass("am-in");
});

function addTag(){
  var tagArr  = $("#tagList .tag").map(function(){
    return $(this).attr("val");
  }).get();
  //console.dir(tagArr);
  var tagText = $.trim($("#newTag").val());
  //console.log($.inArray(tagText,tagArr)>-1);
  if(tagText.length==0){
    mAlert({
      title:"错误",
      text:"不能为空值！",
      onConfirm:function(e) {}
    });
  }else{
    var newTagArr=tagText.split("||");
    for(var i=0;i<newTagArr.length;i++){
      if(newTagArr[i].length>0 && $.inArray(newTagArr[i],tagArr)==-1){
        $("#newTag").val("");
        $("#tagList").prepend('<span val="'+newTagArr[i]+'" class="tag">'+newTagArr[i]+' <a href="javascript:void(0)" class="am-close"  onclick="delTag(this)">&times;</a></span>');
        tagArr.push(newTagArr[i]);
      }
    }
  }
}

function delTag(obj){
  //console.log($(obj).parent(".tag").html());
  $(obj).parent(".tag").remove();
}

function save(){
  var tagArr  = $("#tagList .tag").map(function(){
    return $(this).attr("val");
  }).get().join("||");
  //console.log(tagArr);
  $.post("./savetags", {
    "ajax"			: true,
    "name"			: "<%= name %>",
    "tagArr"		: tagArr
  },function(data){
    //$.AMUI.progress.done();
    $(".btn-saveEdit").removeClass("am-disabled");
    if(typeof(data)=="object" && typeof(data.error)=="undefined"){
      mAlert({
        title:"提示",
        text:"保存成功！",
        onConfirm:function(e) {
          location.reload();
        }
      });
    }else{
      //console.dir(data);
      mAlert({
        title:"错误",
        text:data.error,
        onConfirm:function(e) {}
      });
    }
  }, "json");
}

</script>
